package com.entity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.utility.Rander;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

//其实就是一个node处理器，不要把它当作node本体，JSONObject才是本体
public class Node{
    private static final int IdLen=2;
    //与请求中的node格式一样，唯一不同的是这里的node的children是个对象
    private JSONObject node;
    public Node(JSONObject n){
        node=n;
    }
    public JSONObject getNode() {
        return node;
    }
    //以自己的path为前缀，为
    public void refreshPath(){
        JSONObject children=node.getJSONObject("children");
        if (children!=null){
            Iterator<Map.Entry<String, Object>> it = children.entrySet().iterator();
            while (it.hasNext()){
                Map.Entry<String, Object> next = it.next();
                String path= node.getString("path")+next.getKey();
                JSONObject child = (JSONObject)next.getValue();
                child.put("path",path);
                new Node(child).refreshPath();
            }
        }
    }
    //path字符串为空可返回 根node
    public JSONObject getChildNodeByPath(String path){
        JSONObject targetNode=node;
        for(int i=0;i<path.length();i+=IdLen){
            String id=path.substring(i,i+IdLen);
            targetNode=targetNode.getJSONObject("children").getJSONObject(id);
        }
        return targetNode;
    }
    //简单克隆，不带children
    public JSONObject simpleCloneChildNode(){
        JSONObject targetNode=new JSONObject();
        return targetNode;
    }
    public String appendChild(JSONObject newChild){
        JSONObject children=node.getJSONObject("children");
        if(children==null){
            node.put("children",new JSONObject());
            children=node.getJSONObject("children");
        }

        String id= Rander.getRandomString(IdLen);
        while (children.get(id)!=null)
            id= Rander.getRandomString(IdLen);

        children.put(id,newChild);
        return id;
    }
    //path只有一段路径也可以
    public void deleteChild(String path){
        int l=path.length();
        String id=path.substring(l-IdLen);
        JSONObject fatherNode=getChildNodeByPath(path.substring(0,l-IdLen));
        fatherNode.getJSONObject("children").remove(id);
    }
    public void moveNode(String from,String to){
        JSONObject fromNode=getChildNodeByPath(from);
        Node toNode=new Node(getChildNodeByPath(to));

        toNode.appendChild(fromNode);
        deleteChild(from);
    }
    //以下操作都可以对 子 进行
    public void deleteTagFromNode(String path, String tagId){
        JSONObject child = getChildNodeByPath(path);
        JSONArray tags = child.getJSONArray("tags");
        for (int i=0;i<tags.size();i++){
            String tag= tags.getString(i);
            if(tag.equals(tagId)){
                tags.remove(i);
                break;
            }
        }
    }
    public void addTagToNode(String path, String tagId){
        JSONObject child = getChildNodeByPath(path);
        JSONArray tags = child.getJSONArray("tags");
        tags.add(tagId);
    }
    public JSONObject getTagUnion(String path){
        JSONObject tags=new JSONObject();
        JSONObject targetNode=node;
        for(int i=0;i<path.length();i+=IdLen){
            String id=path.substring(i,i+IdLen);
            targetNode=targetNode.getJSONObject("children").getJSONObject(id);
            //遍历tags数组并put到JSONObject中，用JSONObject可轻松实现取消重复
            JSONArray targetTags=targetNode.getJSONArray("tags");
            for (int j=0;j<targetTags.size();j++){
                //未解决，这里要判断是否是特殊标签，特殊标签的参数刚好填到这个值中
                tags.put(targetTags.getString(j),true);
            }
        }
        return tags;
    }
}
